Optimize tryToDoStringReplacementWithExtraIndentation with first-line fast bailout - #1223
Optimize tryToDoStringReplacementWithExtraIndentation with first-line fast bailout#1223nordicnode wants to merge 1 commit into
Conversation
|
Good find. The fast bailout in The refactor into Minor nit: the PR description claims This is small, in-scope (agent-runtime, not touching forbidden paths), has a plausible benchmark, and includes tests. Worth porting. |
Optimize tryToDoStringReplacementWithExtraIndentation with first-line fast bailout
Summary
• In
packages/agent-runtime/src/generate-diffs-prompt.ts, optimizedtryToDoStringReplacementWithExtraIndentationto eliminate redundant string splits and array joins during indentation mismatch recovery.• Previously, the recovery loop evaluated 1..12 space indents followed by 1..6 tab indents (up to 18 iterations). On every iteration, it called
searchContent.split('\n'), mapped over all lines, and joined them back together. For a 50-line block on non-matching text, this produced 18 full string splits and 900 line mappings.• Pre-split
searchLinesonce outside the loops.• Added a fast-bailout check: if
firstNonEmptyLineis present, it tests whetherprefix + firstNonEmptyLineexists inoldFileContent. If the first indented line is not present, the full block cannot possibly match, bypassing string formatting and joining for that indentation level.• Evaluated
replaceContent.split('\n')lazily only when a match is found.• Verified 100% exact parity across spaces, tabs, leading/trailing empty lines, and partial matches.
• Added unit test coverage for first-line matching with subsequent line mismatch, and leading empty lines in
packages/agent-runtime/src/__tests__/generate-diffs-prompt.test.ts.Verification & Benchmark Results
1. Benchmark
2. Test Suite & Hygiene
bun test packages/agent-runtime/src/__tests__/generate-diffs-prompt.test.tspassed 8/8 tests (0 fail).